package slotMachineGUI;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONObject;

public class JsonData {
     String stringURL =	"http://maps.google.com/maps/api/directions/json?sensor=false";
	 String code;
	 String routeString = "";
	 double latitude;
	 double longtitude;
	 ArrayList<GeoPoint> path;
	 
	 //用json解析google api提供的路徑規劃
	 public JsonData(String source, String destination) throws Exception {
	     	     
	     //解析json的資料，將路徑編碼存在points
		 stringURL += "&origin=" + source + "&destination=" + destination;
		 String jsonOutput = JsonData.sentHttpRequest(stringURL);
		 JSONObject jsonObject = new JSONObject(jsonOutput);
		 JSONObject route = jsonObject.getJSONArray("routes").getJSONObject(0);
		 String points = route.getJSONObject("overview_polyline").getString("points");
         
		 path = decodePoly(points);               //解析路徑編碼產生path
         for (int i = 0; i < path.size(); i++) {  //將路線資訊串接起來
             latitude = (double)path.get(i).getLatitude()/1E6;
        	 longtitude = (double)path.get(i).getLongtitude()/1E6;
        	 
        	 if ( i == path.size() - 1 ) {
        	     routeString += 
        	             String.valueOf(latitude) + "," + String.valueOf(longtitude);
        	 }    
        	 else {
        	     routeString += 
        	             String.valueOf(latitude) + "," + String.valueOf(longtitude) + "|";
        	 }
         }
	 }
	 
	 public String getRouteString() {
	     return routeString;   //回傳串接好得路線
	 }
	 
	 //解析google api的路徑編碼，並存成ArrayList
	 private ArrayList<GeoPoint> decodePoly(String encoded) {
	     ArrayList<GeoPoint> poly = new ArrayList<GeoPoint>();   
	     int index = 0, len = encoded.length();   
	     int lat = 0, lng = 0;
	     while (index < len) {
	         int b, shift = 0, result = 0;
	         do {
	             b = encoded.charAt(index++) - 63;
	             result |= (b & 0x1f) << shift;   
	             shift += 5;
	         } while (b >= 0x20);
	         int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
	         lat += dlat;
	         shift = 0;
	         result = 0;
	         do { 
	             b = encoded.charAt(index++) - 63; 
	             result |= (b & 0x1f) << shift;   
	             shift += 5;
	         } while (b >= 0x20); 
	         int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
	         lng += dlng;
	         GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6),
	                 (int) (((double) lng / 1E5) * 1E6));
	         poly.add(p);
	     }
	     return poly; 
	 }
	 
	 //以url取得網頁原始碼
	 public static String sentHttpRequest(String url) throws Exception {
	     try {
	         URL pageUrl = new URL(url);
	         URLConnection conn = pageUrl.openConnection();
	         BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	         StringBuffer sb = new StringBuffer("");
	         String line="";
	         String NL =System.getProperty("line.separator");
	         
	         while((line=reader.readLine())!= null){
                 sb.append(line+NL); 
             }
             String result=sb.toString();
             return result;
	     } catch(Exception e) {
	         e.getMessage();
	         e.printStackTrace();
	         return null;
	     }
	 }  
}
